<?php if (!defined('ROOT_DIR')) exit('No direct script access!');

/**
 * @author Ervin R. Musngi <ervinmusngi@gmail.com>
 * @copyright Copyright (c) Net-Core-Tech 2011 Ervin Musngi
 * @version 1.0.1
 * @dateCreated - November 22,2011
 * @dateModified - November 22, 2011
 */

class Form_validation {
    
    public static $instance;
    
    private $_rules;
    
    private $_result;
    
    private $_values;
    
    public function __construct()
    {
        self::$instance = $this;
    }
    
    public function set_rules( $array = '' )
    {
        
        if ( ! isset( $array ) && ! is_array( $array )  )
        {
            return false;
        }
        
        $this->_rules = $array;
        $this->_result = null;

    }
    
    public function run()
    {
        
        $i = 0;
        
        foreach ( $this->_rules as $rule )
        {
            $rules_s = array();
            
            foreach ( $rule['rules'] as $r )
            {
                $result = call_user_func_array(array($this,'validation_'.$r),array($rule['field'],$rule['label']));
                
                if ( ! $result['result'] )
                {
                    $rules_s[] = $result['message'];
                    $i++;
                }
                
            }
            $this->_values[$rule['field']] = $_POST[$rule['field']];
            $this->_result[$rule['field']] = implode(' | ',$rules_s);
            
            
        }
        
        if ( $i > 0 ) {
            return false;
        } else {
            return true;
        }
        
    }
    
    public function get_result()
    {
        return $this->_result;
    }
    
    public function get_values()
    {
        return $this->_values;
    }
    
    public function validation_or_check( $key = '', $label = '' )
    {
        $con = Connect::$instance;
        
        $val = $_POST[$key];
        
        $result =   $con->select(' COUNT(`id`) AS `num` ',FALSE)
                        ->from('user')
                        ->where('or_number',$val)
                        ->get();
        
        $row = $result->row();
        
        if ( $row->num > 0 )
        {
            return array('result'=>false,'message'=>' The '. $label.' you entered is already registered!');
        }
        else
        {
            return array('result'=>true);
        }
        
    }

    public function validation_required( $key = '', $label = '' )
    {
        if ( ! isset ($key) || ! isset($_POST[$key]) || empty($_POST[$key]) )
        {
            return array('result'=>false,'message'=>$label.' is Required!');
        } else {
            return array('result'=>true);
        }
    }
    
    public function validation_date( $key , $label = '' ,$format = "MM/DD/YYYY" )
    {
        if ( !isset($_POST[$key]) || empty($_POST[$key]) )
        {
            return array('result'=>false,'message'=>'Invalid date format, use ( MM/DD/YYYY ) for '.$label.'!'); 
        } else {
            $res = $_POST[$key];
        }
        $y = 0;
        $m = 0;
        $d = 0;
        switch( $format )
        {
            case 'YYYY/MM/DD':
            case 'YYYY-MM-DD':
                
            $a = preg_split( '/[-\.\/ ]/', $res );
            
            if ( count($a) === 3 )
            {
                list( $d, $m, $y ) = $a;
            }
            
            break;

            case 'YYYY/DD/MM':
            case 'YYYY-DD-MM':
            
            $a = preg_split( '/[-\.\/ ]/', $res );
            
            if ( count($a) === 3 )
            {
                list( $d, $m, $y ) = $a;
            }
            
            break;

            case 'DD-MM-YYYY':
            case 'DD/MM/YYYY':
                
            $a = preg_split( '/[-\.\/ ]/', $res );
            
            if ( count($a) === 3 )
            {
                list( $d, $m, $y ) = $a;
            }
            
            break;

            case 'MM-DD-YYYY':
            case 'MM/DD/YYYY':
            
            $a = preg_split( '/[-\.\/ ]/', $res );
            
            if ( count($a) === 3 )
            {
                list( $d, $m, $y ) = $a;
            }
            
            break;

            case 'YYYYMMDD':
            $y = substr( $res, 0, 4 );
            $m = substr( $res, 4, 2 );
            $d = substr( $res, 6, 2 );
            break;

            case 'YYYYDDMM':
            $y = substr( $res, 0, 4 );
            $d = substr( $res, 4, 2 );
            $m = substr( $res, 6, 2 );
            break;

            default:
            throw new Exception( "Invalid Date Format" );
        }
        
        if ( is_string($y) || is_string($d) || is_string($y)  || ! checkdate( $m, $d, $y ) )
        {
            return array('result'=>false,'message'=>'Invalid date format, use ( MM/DD/YYYY ) for '.$label.'!'); 
        }
        else
        {
            return array('result'=>true);
        }
        
    }
    
    
}